﻿
#include "LocationViewController.h"

LocationViewController::LocationViewController() : m_iLocNum(0), m_iCode(0)
{

}

LocationViewController::~LocationViewController()
{

}

void LocationViewController::viewDidLoad()
{
    // Do any additional setup after loading the view from its nib.

	this->setNavigationBarItem(CANavigationBarItem::create(UTF8("定位")));

	const DSize& viewSize = this->getView()->getFrame().size;

	AppTool::getInstance()->setAmapLocationDelegate(this);

	CAButton* oncebt = CAButton::createWithFrame(DRect(50, 100, 200, 90), CAButtonTypeRoundedRect);
	oncebt->setTitleFontSize(30);
	oncebt->setTitleForState(CAControlStateAll, UTF8("单次定位"));
	oncebt->setTitleColorForState(CAControlStateAll, CAColor_black);
	oncebt->addTarget(this, CAControl_selector(LocationViewController::onceLocBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(oncebt);

	lb1 = CALabel::createWithFrame(DRect(20, 100 + 100, viewSize.width - 20 * 2, 300));
	lb1->setFontSize(30);
	lb1->setColor(CAColor_black);
	lb1->setTextAlignment(CATextAlignmentLeft);
	lb1->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lb1->setText(UTF8("单次定位结果"));
	this->getView()->addSubview(lb1);

	CAButton* locbt = CAButton::createWithFrame(DRect(50, 100 + 100 + 300, 200, 90), CAButtonTypeRoundedRect);
	locbt->setTitleFontSize(30);
	locbt->setTitleForState(CAControlStateAll, UTF8("持续定位"));
	locbt->setTitleColorForState(CAControlStateAll, CAColor_black);
	locbt->addTarget(this, CAControl_selector(LocationViewController::locBtClicked), CAControlEventTouchUpInSide);
	this->getView()->addSubview(locbt);

	lb2 = CALabel::createWithFrame(DRect(20, 100 + 100 + 300 + 100, viewSize.width - 20 * 2, 300));
	lb2->setFontSize(30);
	lb2->setColor(CAColor_black);
	lb2->setTextAlignment(CATextAlignmentLeft);
	lb2->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	lb2->setText(UTF8("持续定位结果"));
	this->getView()->addSubview(lb2);
}

void LocationViewController::viewDidUnload()
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

void LocationViewController::onceLocBtClicked(CAControl* ctrl, DPoint point)
{
	AppTool::getInstance()->startOnceLocation();
}

void LocationViewController::locBtClicked(CAControl* ctrl, DPoint point)
{
	AppTool::getInstance()->startLocation();
}

void LocationViewController::onOnceLocationEnd(int code, double lat, double lng, const string& formatAddr, const string& province, const string& city, const string& district, const string& street, const string& number, const string& poi, const string& adcode)
{
	CCLog("void LocationViewController::onOnceLocationEnd---------------------");

	m_iCode = code;
	m_DLat = lat;
	m_DLng = lng;
	CAScheduler::schedule(schedule_selector(LocationViewController::oncelocSchedule), this, 0);
}

void LocationViewController::onLocationUpdate(int code, double lat, double lng, const string& formatAddr, const string& province, const string& city, const string& district, const string& street, const string& number, const string& poi, const string& adcode)
{
	CCLog("void LocationViewController::onLocationUpdate---------------------");

	m_iLocNum++;
	m_iCode = code;
	m_DLat = lat;
	m_DLng = lng;
	CAScheduler::schedule(schedule_selector(LocationViewController::locSchedule), this, 0);
}

void LocationViewController::oncelocSchedule(float t)
{
	CAScheduler::unschedule(schedule_selector(LocationViewController::oncelocSchedule), this);

	if (m_iCode == 0)
	{
		lb1->setText(UTF8("单次定位结果：\n") + crossapp_format_string("Lat:%f   Lng:%f", m_DLat, m_DLng));
	}
	else
	{
		lb1->setText(UTF8("单次定位失败"));
	}
}

void LocationViewController::locSchedule(float t)
{
	CAScheduler::unschedule(schedule_selector(LocationViewController::locSchedule), this);

	if (m_iCode == 0)
	{
		lb2->setText(UTF8("持续定位结果：\n") + crossapp_format_string("num:%d\nLat:%f   Lng:%f", m_iLocNum,m_DLat, m_DLng));
	}
	else
	{
		lb2->setText(UTF8("持续定位失败"));
	}
}

